/*
 * Copyright (C) 2013 Michael
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package com.jm.jmsql.tabulation;

import com.jm.jmsql.JMSql;
import java.awt.Font;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;

/**
 * 
 * 
 * @created Jan 17, 2013
 * @author Michael L.R. Marques
 */
public class TabPageHeader extends javax.swing.JPanel {
    
    /**
     * 
     */
    private JMSql app;
    
    /**
     * 
     */
    private JTabbedPane parent;
    
    /**
     * 
     */
    private JComponent component;
    
    /**
     * 
     */
    private boolean edited;
    
    /**
     * 
     * @param app
     * @param parent
     * @param text
     * @param toolTipText 
     */
    public TabPageHeader(JMSql app, JTabbedPane parent, JComponent component, String text) {
        this(app, parent, component, text, text);
    }
    
    /**
     * 
     * @param app
     * @param parent
     * @param text
     * @param toolTipText 
     */
    public TabPageHeader(JMSql app, JTabbedPane parent, JComponent component, String text, String toolTipText) {
        initComponents();
        this.app = app;
        this.parent = parent;
        this.component = component;
        this.lblHeader.getFont().deriveFont((this.edited = false) ? Font.BOLD : Font.PLAIN);
        this.lblHeader.setText(text);
        this.lblHeader.setToolTipText(toolTipText);
    }
    
    /**
     * 
     * @param text 
     */
    public void setText(String text) {
        this.lblHeader.setText(text);
    }
    
    /**
     * 
     * @return 
     */
    public String getText() {
        return this.lblHeader.getText();
    }
    
    /**
     * 
     * @param text 
     */
    @Override public void setToolTipText(String text) {
        this.lblHeader.setToolTipText(text);
    }
    
    /**
     * 
     * @return 
     */
    @Override public String getToolTipText() {
        return this.lblHeader.getToolTipText();
    }
    
    /**
     * 
     * @param font 
     */
    public void setEdited(boolean edited) {
        this.lblHeader.setFont(this.lblHeader.getFont().deriveFont((this.edited = edited) ? Font.BOLD : Font.PLAIN));
    }
    
    /**
     * 
     */
    public boolean getEdited() {
        return this.edited;
    }
    
    /**
     * 
     * @param icon 
     */
    public void setIcon(ImageIcon icon) {
        this.lblHeader.setIcon(icon);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblHeader = new javax.swing.JLabel();
        btnClose = new javax.swing.JButton();

        setOpaque(false);

        lblHeader.setFont(new java.awt.Font("Arial", 0, 11)); // NOI18N
        lblHeader.setText("New.sql");
        lblHeader.setToolTipText("");
        lblHeader.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                headerClicked(evt);
            }
        });

        btnClose.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/jm/jmsql/icons/tab_close_disabled.png"))); // NOI18N
        btnClose.setBorder(null);
        btnClose.setBorderPainted(false);
        btnClose.setContentAreaFilled(false);
        btnClose.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/com/jm/jmsql/icons/tab_close.png"))); // NOI18N
        btnClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                headerCloseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(lblHeader, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnClose))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(lblHeader, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(btnClose, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void headerCloseClicked(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_headerCloseClicked
       this.parent.setSelectedComponent(this.component);
       this.parent.removeTabAt(this.parent.getSelectedIndex());
    }//GEN-LAST:event_headerCloseClicked

    private void headerClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_headerClicked
        this.parent.setSelectedComponent(this.component);
        if (SwingUtilities.isRightMouseButton(evt)) {
           TabPagePopupMenu.show(this.app, this.parent, (JComponent) evt.getSource(), evt.getX(), evt.getX());
        }
    }//GEN-LAST:event_headerClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnClose;
    private javax.swing.JLabel lblHeader;
    // End of variables declaration//GEN-END:variables
    
}
